package com.demo.bigdata.sql

import org.apache.spark.sql.{SaveMode, SparkSession}

import java.text.SimpleDateFormat

object UDFDemo {
  def main(args: Array[String]): Unit = {
    val spark = SparkSession
      .builder()
      .config("spark.sql.warehouse.dir","")
      .enableHiveSupport()
      .appName("UDF Demo")
      .master("local")
      .getOrCreate()

    val dateFormat =  new SimpleDateFormat("yyyy")
    spark.udf.register("getYear", (date:Long) => dateFormat.format(date).toInt)
    val df = spark.sql("select getYear(date_key) year, * from etl_fxdj")
    df.write.mode(SaveMode.Overwrite).saveAsTable("dw_dm_fx_fxdj")
  }
}
